package com.lyz.controller;

import com.lyz.domain.dto.BookPushDTO;
import com.lyz.domain.dto.BookQueryDTO;
import com.lyz.domain.entity.Book;
import com.lyz.domain.po.ResponseEntity;
import com.lyz.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("books")
public class BookController {
    @Autowired
    IBookService bookService;

    @PostMapping("add")
    public ResponseEntity add(@RequestBody BookPushDTO bookPush) {
        boolean state = bookService.add(bookPush);
        return state ? ResponseEntity.success(bookPush)
                : ResponseEntity.error("添加图书失败，请重试...");
    }

    @DeleteMapping("remove/{isbn}")
    public ResponseEntity remove(@PathVariable String isbn) {
        boolean state = bookService.remove(isbn);
        return state ? ResponseEntity.success()
                : ResponseEntity.error("移除图书（isbn：" + isbn + "）失败");
    }

    @GetMapping("find")
    public ResponseEntity find(@RequestBody BookQueryDTO bookQuery) {
        List<Book> books = bookService.find(bookQuery);
        return books == null || books.isEmpty() ? ResponseEntity.error("拉取图书列表出错")
                : ResponseEntity.success(books);
    }
}
